import { Component, Injector } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { CommonLookupServiceProxy, CreateDirectoryInput, DirectoryItem, FileLibraryServiceProxy, IPathItem, NameValueDtoOfInt64,  } from '@shared/service-proxies/service-proxies';
import { BsModalRef } from 'ngx-bootstrap/modal';
import { merge as _merge } from 'lodash-es';
import { finalize } from 'rxjs/operators';

@Component({
    templateUrl: './directory-create-modal.component.html'
})
export class DirectoryCreateModalComponent extends AppComponentBase {

    openNewDir: {(result: DirectoryItem):void};

    saving: boolean = false;

    originName: string;

    data: CreateDirectoryInput ;

    usersData:NameValueDtoOfInt64[] =[];
    pMutiSelectedValue:NameValueDtoOfInt64[] =[];
    defaultLabel: string;

    parentName: string;

    constructor(
        injector: Injector,
        public bsModalRef: BsModalRef,
        private _fileLibraryService: FileLibraryServiceProxy,
        private _commonLookupServiceProxy:CommonLookupServiceProxy
    ) {
        super(injector);
        
        this.data = new CreateDirectoryInput();
        this.defaultLabel = this.l("NoLimit");
    }

    close(): void {
        this.bsModalRef.hide();
    }

    shown(parent:IPathItem = null): void {
        this.data = new CreateDirectoryInput();
        if(parent){
            this.data.parentId = parent.id;
            this.parentName = parent.displayName;
        }else{
            this.data.parentId = null;
            this.parentName = '';
        }

        this._commonLookupServiceProxy.findUsers2()
            .subscribe(result => {
                this.usersData = result;
            });
    }

    save() {
        if (this.saving) {
            return;
        }
        this.saving = true;

        this.data.userIds = this.pMutiSelectedValue.map(tt=> tt.value);

        this._fileLibraryService.createDirectory(this.data)
            .pipe(finalize(() => this.saving = false))
            .subscribe(result =>{
                this.notify.success(this.l('SavedSuccessfully'));
                this.openNewDir && this.openNewDir(result);
                this.close();
            });
    }
}
